package com.zackeus.sipservice;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.zackeus.mvvm.event.RxBus;
import com.zackeus.sipservice.constant.Event;
import com.zackeus.sipservice.entity.RtpStreamStats;
import com.zackeus.sipservice.entity.SipServiceEvent;


/**
 * SIP 事件广播器
 * @name: SipServiceEventEmitter
 * @author: zz
 * @date: 2022/9/20
 */
public class SipServiceEventEmitter {

    private static final String LOG_TAG = SipServiceEventEmitter.class.getSimpleName();
    private final Gson gson = new Gson();
    private final Context mContext;

    public SipServiceEventEmitter(Context context) {
        mContext = context;
    }

    /**
     * 呼叫重新连接
     * @param callId 呼叫ID
     * @param state 状态 (true: 连接成功; false: 连接失败)
     */
    void callReconnectionState(int callId, boolean state) {
        JsonObject event = new JsonObject();
        event.addProperty("callId", callId);
        event.addProperty("state", state);

        RxBus.getInstance().post(new SipServiceEvent(Event.EVENT_CALL_RECONNECTION_STATE, event.toString()));
    }

    /**
     * 注册/注销
     * @param accountId SIP 账号ID
     * @param renew
     *  true: 注册
     *  false: 注销
     */
    void registrationStarted(String accountId, boolean renew) {
        JsonObject event = new JsonObject();
        event.addProperty("accountId", accountId);
        event.addProperty("renew", renew);

        RxBus.getInstance().post(new SipServiceEvent(Event.EVENT_REGISTRATION_STARTED, event.toString()));
    }

    /**
     * 注册状态
     * @param accountId SIP 账号ID
     * @param status 注册业务状态
     * @param statusCode SIP状态码
     * @param reason SIP原因短语
     */
    void registrationState(String accountId, int status, int statusCode, String reason) {
        JsonObject event = new JsonObject();
        event.addProperty("accountId", accountId);
        event.addProperty("status", status);
        event.addProperty("statusCode", statusCode);
        event.addProperty("reason", reason);

        RxBus.getInstance().post(new SipServiceEvent(Event.EVENT_REGISTRATION_STATE, event.toString()));
    }

    /**
     * 会话状态
     * @param accountId SIP 账号ID
     * @param callId 会话唯一标识
     * @param callRole 会话方向
     * @param callState SIP 呼叫状态
     * @param callStatus SIP 呼叫状态码
     * @param confirmedTimestamp 通话建立时间戳 (未建立时为0)
     */
    void callState(String accountId, int callId, int callRole, int callState, int callStatus, long confirmedTimestamp) {
        JsonObject event = new JsonObject();
        event.addProperty("accountId", accountId);
        event.addProperty("callId", callId);
        event.addProperty("callRole", callRole);
        event.addProperty("callState", callState);
        event.addProperty("callStatus", callStatus);
        event.addProperty("confirmedTimestamp", confirmedTimestamp);

        RxBus.getInstance().post(new SipServiceEvent(Event.EVENT_CALL_STATE, event.toString()));
    }

    /**
     * 通话统计
     * @param accountId SIP 账号ID
     * @param callId 会话唯一标识
     * @param connectTime 呼叫连接持续时长 (呼叫未建立时为零)
     * @param totalTime 总时长 包括设置时间
     * @param audioCodec 音频编码
     * @param hangupStatus 挂断状态码
     * @param rx 解码器流统计
     * @param tx 编码器流统计
     */
    void callStats(String accountId, int callId, int connectTime , int totalTime,
                   String audioCodec, int hangupStatus,
                   RtpStreamStats rx, RtpStreamStats tx) {
        JsonObject event = new JsonObject();
        event.addProperty("accountId", accountId);
        event.addProperty("callId", callId);
        event.addProperty("connectTime", connectTime);
        event.addProperty("totalTime", totalTime);
        event.addProperty("audioCodec", audioCodec);
        event.addProperty("hangupStatus", hangupStatus);
        event.addProperty("rxStat", gson.toJson(rx));
        event.addProperty("txStat", gson.toJson(tx));
        RxBus.getInstance().post(new SipServiceEvent(Event.EVENT_CALL_STATS, event.toString()));
    }

    /**
     * 外呼
     * @param accountId
     * @param callId
     * @param userName
     * @param displayName
     */
    void outgoingCall(String accountId, int callId, String userName, String displayName) {
        JsonObject event = new JsonObject();
        event.addProperty("accountId", accountId);
        event.addProperty("callId", callId);
        event.addProperty("userName", userName);
        event.addProperty("displayName", displayName);
        RxBus.getInstance().post(new SipServiceEvent(Event.EVENT_OUTGOING_CALL, event.toString()));
    }

    /**
     * 来电
     * @param accountId
     * @param callId
     * @param userName
     * @param displayName
     */
    void incomingCall(String accountId, int callId, String userName, String displayName) {
        JsonObject event = new JsonObject();
        event.addProperty("accountId", accountId);
        event.addProperty("callId", callId);
        event.addProperty("userName", userName);
        event.addProperty("displayName", displayName);
        RxBus.getInstance().post(new SipServiceEvent(Event.EVENT_INCOMING_CALL, event.toString()));
    }
}
